var currentpage = 1, totalpage = 1, pagesize = 10;
$(function () {
	if (UserID && UserID != "null") {
		getsetCourse(1);
	}
});
//获取设置课程列表
function getsetCourse(pg) {
	currentpage = pg;
	var sucessfct = function (data) {
		currentpage = data.currentPageNo;
		totalpage = data.totalPageCount;
		pagesize = data.pagesize;
		setCourseContext(data.result);
	};
	$.ajax({
		url: "publishlessons/queryresult",
		type: "POST",
		contentType: "application/json",
		data: JSON.stringify({
			owner: UserID,
			pageindex: currentpage,
			pagesize: pagesize,
			modeid: 7
		}),
		dataType: "json",
		success: sucessfct,
		error: function (event) {
			window.parent.parentLayermsg(langThis("role_optionfaild"));
			$('#container>img[alt="loading"]').fadeOut();
			// console.log(event);
		}
	});
}
function setCourseContext(data) {
	if (!data.length) {
		$("#courselistcan").html('<div class="askcreatcourse"><p><span class="glyphicon glyphicon-plus" aria-hidden="true"></span><a target="_blank" href="./cOmCourse">' + langThis("kc_creat_course") + '</a></p></div>');
		$("#partpagenavbar").fadeOut();
		$('#container>img[alt="loading"]').fadeOut();
		return;
	}
	var context = '<ul class="media-list">';
	for (var i in data) {
		context += '<li class="media lessonbottomHr" data-lessonid="' + data[i].id + '" data-phase="' + data[i].phase + '">';
		context += '<a class="media-left" target="_blank" href="./lessonDetail?lid=' + data[i].id + '&p=' + data[i].phase + '">';
		context += '<img style="cursor:pointer;" src="' + data[i].image + '"  class="lessonimg"></a>';
		context += '<div class="media-body"><h3 class="media-heading lessonTitle">' + data[i].name + '</h3>';
		context += '<p class="lessontxt">' + data[i].description + '</p>';
		context += '<a title="' + langThis("lc_workcorrect") + '" type="button" class="btn btn-primary" target="_blank" href="./CSCorrecting?lid=' + data[i].id + '&p=' + data[i].phase + '"><span class="glyphicon glyphicon-th-list"></span>' + langThis("lc_workcorrect") + '</a>';

		//开课批次，待发布和1不显示
		var phaseString="";
		if (data[i].phase && data[i].phase != 1) {
			phaseString = '<span class="badge lessonSstag">' + langThis("cm_coursePhase").replace("{p}", data[i].phase) + '</span> ';
		}
		//课程类型
		context +='<span class="classPhaseSpan">'+ phaseString + ' <span class="badge lessonTypeTag">'+(/zh/.test(getCurrentLang())?data[i].typenamecn:data[i].typenameen)+'</span></span>';
	}
	context += '</ul>';
	$("#courselistcan").html(context);
	$('#container>img[alt="loading"]').fadeOut();
	setpartpagebar();
	// 子页面设置父页面iframe高度
	if (window.parent.setiframeHeight) {
		var ht = $("#container").height();
		ht = ht > 800 ? ht : 800;
		window.parent.setiframeHeight(ht);
	}
}
//分页控制
function setpartpagebar() {
	var pagesli = '';
	//首页、上一页
	if (currentpage == 1 || totalpage == 1) {
		pagesli += '<li title="' + langThis("nav_master") + '" class="active"><a>&laquo;</a></li>';
		pagesli += '<li title="' + langThis("nav_pre") + '" class="active"><a>&lt;</a></li>';
	} else {
		pagesli += '<li title="' + langThis("nav_master") + '"><a style="cursor:pointer;" onclick="gotofirstpage()">&laquo;</a></li>';
		pagesli += '<li title="' + langThis("nav_pre") + '"><a style="cursor:pointer;" onclick="previouspage()">&lt;</a></li>';
	}
	//页码
	var startpage;
	if (currentpage - 5 <= 0) {
		startpage = 1;
	} else if (currentpage + 5 > totalpage) {
		startpage = totalpage - 9;
	} else {
		startpage = currentpage - 4;
	}
	// 左过长
	if (startpage > 1) {
		pagesli += '<li><a style="cursor:pointer;" onclick="gotopage(' + (startpage - 1) + ')">...</a></li>';
	} else {
		startpage = 1;
	}
	for (var j = startpage; j <= totalpage; j++) {
		if (j == currentpage) {
			pagesli += '<li class="active"><a>' + j + '</a></li>';
		} else {
			pagesli += '<li><a style="cursor:pointer;" onclick="gotopage(' + j + ')">' + j + '</a></li>';
		}
		//右过长
		if ((j == startpage + 9) && (j < totalpage)) {
			pagesli += '<li><a style="cursor:pointer;" onclick="gotopage(' + (startpage + 10) + ')">...</a></li>';
			break;
		}
	}
	//下一页、尾页
	if (currentpage == totalpage || totalpage == 1) {
		pagesli += '<li title="' + langThis("nav_next") + '" class="active"><a>&gt;</a></li>';
		pagesli += '<li title="' + langThis("nav_last") + '" class="active"><a>&raquo;</a></li>';
	} else {
		pagesli += '<li title="' + langThis("nav_next") + '"><a style="cursor:pointer;" onclick="nextpage()">&gt;</a></li>';
		pagesli += '<li title="' + langThis("nav_last") + '"><a style="cursor:pointer;" onclick="gotolastpage()">&raquo;</a></li>';
	}
	$("#partpagenavbar>ul.pagination").html(pagesli);
	$("#partpagenavbar").fadeIn();
}
function previouspage() {
	getsetCourse(currentpage--);
}
function nextpage() {
	getsetCourse(currentpage++);
}
function gotopage(pagenum) {
	getsetCourse(pagenum);
}
function gotofirstpage() {
	getsetCourse(1);
}
function gotolastpage() {
	getsetCourse(totalpage);
}
// 翻页快捷键
document.onkeydown = function (e) {
	if ((e || event).keyCode == 37 && currentpage > 1) {//上一页
		previouspage();
	} else if ((e || event).keyCode == 39 && currentpage < totalpage) {//下一页
		nextpage();
	} else if ((e || event).keyCode == 36 && currentpage != 1) {//首页
		gotofirstpage();
	} else if ((e || event).keyCode == 35 && currentpage != totalpage) {//尾页
		gotolastpage();
	}
};